package org.myorg.v4;


import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;

import org.myorg.v6.MountainRecordWritable;

public class FPeople4 implements Serializable {
	
	public Long it = new Long(0);
	public MountainRecordWritable rec = new  MountainRecordWritable();
	
	public int p_num;
	public FGene4 persons[];

	public FPeople4() {
		p_num = 0;
		rec = new  MountainRecordWritable();
	}
	
	public FPeople4(int num,int length) {
		p_num = num;
		persons = new FGene4[num];

 		for(int i = 0; i < num; i++) {
			persons[i] = new FGene4(length);
		}
 		rec = new  MountainRecordWritable();
	}
	
	public FPeople4(int num) {
		p_num = num;
		persons = new FGene4[num];
		rec = new  MountainRecordWritable();
	}
	
	public FPeople4(FPeople4 people) {
		this.it = people.it;
		this.p_num = people.p_num;
		this.persons = new FGene4[people.p_num];
		for ( int i = 0; i < people.p_num; i++){
			this.persons[i] = new FGene4(people.persons[i]);
		}
		rec = new  MountainRecordWritable(people.rec);
	}

	public void add(FPeople4 p) {
				
		ArrayList<FGene4> all_p =  new ArrayList<FGene4>(0);
		
		for (int i = 0; i < this.p_num; i++) {
			all_p.add(this.persons[i]);
		}
		
		for (int i = 0; i < p.p_num; i++) {
			all_p.add(p.getPersons()[i]);
		}
		
		this.p_num = this.p_num + p.p_num;

		this.persons = all_p.toArray(new FGene4[all_p.size()]);
		

		rec = new MountainRecordWritable(p.rec);


		
	}
	
	public FPeople4[] seg(int num) {
		FPeople4[] ap = new FPeople4[num];
		int size = p_num/num;
		for (int i = 0; i < num ; i++) {
			
			ap[i] = new FPeople4(size);
			ap[i].it = it;
			for (int j = 0; j < size; j++) {
				ap[i].persons[j] = new FGene4(persons[i*size + j]);
			}
			
			ap[i].rec = new  MountainRecordWritable(rec);
		}
		
		return ap;
	}
	
	public FGene4 getBest() {
		FGene4 res = persons[0];
		for (int i = 1; i < p_num; i++) {
			if(persons[i].fitness > res.fitness )
				res = persons[i];
		}
		return res;
	}
	
	public void addit() {
		it++;
	}
	
	public FGene4[] getPersons() {
		return persons;
	}
	
	public FGene4 personAt(int index) {
		return persons[index];
	}
	
	public void putPerson(FGene4 gene, int index) {
		persons[index] = new FGene4(gene);
	}

}

